home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testudpclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  3.2 KB  |  176 lines  |  [TEXT/MPS ]

  1. #include <Events.h>
  2. #include <memory.h>
  3. #include <types.h>
  4. #include <OSUtils.h> /* for SysBeep */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/errno.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14. #include <sys/uio.h>
  15.  
  16. #include "tcpglue.h"
  17. #include "socket.internal.h"
  18.  
  19. static struct timeval selectPoll = {0,0};
  20. main()
  21. {
  22.     int status;
  23.     int s;
  24.     struct sockaddr_in me, her, name,from;
  25.     int namelen;
  26.     int herlen = sizeof(her);
  27.     int fromlen = sizeof(from);
  28.     int bytes;
  29.     char line[1000];
  30.     int count,readfds, writefds, exceptfds;
  31.     
  32. #if 1
  33.     dprintf("address %08x\n",xIPAddr());
  34.     dprintf("netmask %08x\n",xNetMask());
  35.     dprintf("max mtu %d\n",xMaxMTU());
  36. #endif
  37.  
  38.     /* make our socket */
  39.     s = s_socket(AF_INET, SOCK_DGRAM, 0);
  40.     if (s < 0)
  41.     {
  42.         perror("socket");    
  43.         exit(1);
  44.     }
  45.     
  46.     /* bind it to port zero - ie. let MacTCP pick a port */
  47.     bzero((char *)&me, sizeof(me));
  48.     me.sin_family = AF_INET;
  49.     me.sin_port = htons(25);
  50.     if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
  51.     {
  52.         perror("bind");    
  53.         exit(1);
  54.     }
  55.     
  56. #if 1
  57.     /* go non-blocking */
  58.     if (s_ioctl(s,FIONBIO,0) < 0)
  59.     {
  60.         perror("ioctl(FIONBIO)");    
  61.         exit(1);
  62.     }
  63. #endif
  64.  
  65.     /* set the address of the server */
  66.     her.sin_family = AF_INET;
  67.     her.sin_addr.s_addr = /* 0x0d000ce8 */ 0x8064660a;
  68.     her.sin_port = htons(69); /* TFTP */
  69.  
  70.     /* send a request - a TFTP RRQ (read request) */
  71.     udpCheckNotify();
  72.     BlockMove("\000\001rubbish\000netascii\000",line,19);
  73.     if (s_sendto(s,line,19/*bytes*/,0/*flags*/,&her,herlen) <= 0 && errno != EINPROGRESS)
  74.     {
  75.         perror("send");
  76.         exit(1);
  77.     }
  78.     
  79.     /* wait for it to get there */
  80.     for (;;)
  81.     {
  82.         udpCheckNotify();
  83.         writefds = 0xffffffff;
  84.         count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  85.         if (count < 0)
  86.         {
  87.             perror("select");
  88.             exit(1);
  89.         }
  90.         if (count > 0)
  91.             break;
  92.     }
  93.  
  94.     for (;;)
  95.     {
  96.         /* wait for the response */
  97.         for (;;)
  98.         {
  99.             udpCheckNotify();
  100.             readfds = 0xffffffff;
  101.             count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
  102.             if (count < 0)
  103.             {
  104.                 perror("select");
  105.                 exit(1);
  106.             }
  107.             if (count > 0)
  108.                 break;
  109.         }
  110.     
  111.         /* retreive the response */
  112.         udpCheckNotify();
  113.         bytes = s_recvfrom(s,line,sizeof(line)-2,0/*flags*/,&from,&fromlen);
  114.         if (bytes < 0)
  115.         {
  116.             perror("read");
  117.             exit(1);
  118.         }
  119.         dprintf("recv got %d bytes from %08x/%d\n",
  120.                 bytes,from.sin_addr.s_addr,from.sin_port);
  121.  
  122. #ifdef 0
  123.         hex_dump(line,16,"");
  124. #endif
  125.         
  126.         /* note who answered us - probably a different port */
  127.         /* only needs to be done the first time around */
  128.         BlockMove((Ptr)&from,(Ptr)&her,herlen);
  129.  
  130.         if (line[1] != 3/*data block*/)
  131.             break;
  132.             
  133.         /* wow! the file exists and we can read it - send an ack */
  134.         udpCheckNotify();
  135.         line[1] = 4/*ack*/;
  136.         if (s_sendto(s,line,4/*bytes*/,0/*flags*/,&her,herlen) <= 0 && errno != EINPROGRESS)
  137.         {
  138.             perror("send");
  139.             exit(1);
  140.         }
  141.         
  142.         /* wait for it to get there */
  143.         for (;;)
  144.         {
  145.             udpCheckNotify();
  146.             writefds = 0xffffffff;
  147.             count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  148.             if (count < 0)
  149.             {
  150.                 perror("select");
  151.                 exit(1);
  152.             }
  153.             if (count > 0)
  154.                 break;
  155.         }
  156.         
  157.         if (bytes < 516)
  158.             break;
  159.     }
  160.  
  161.     /* clean up */
  162.     udpCheckNotify();
  163.     if (s_close(s) < 0)
  164.     {
  165.         perror("close(s)");
  166.         exit(1);
  167.     }
  168.     
  169.     /* spin in case something odd happens at the end */
  170.     for (;;)
  171.     {
  172.         udpCheckNotify();
  173.     }
  174. }
  175.  
  176.